package cn.thoughtworks.school.repositories;

import cn.thoughtworks.school.entities.Video;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface VideoRepository extends JpaRepository<Video,Long> {
  List<Video> findByCreatorIdAndOrganizationId(Long id, Long organizationId);

  List<Video> findFirst8ByIsAllVisibleIsTrueAndIsReleaseIsTrueOrOrganizationIdAndIsReleaseIsTrueOrderByCreateTimeDesc(Long organizationId);

  Page<Video> findByIsAllVisibleIsTrueAndIsReleaseIsTrueOrOrganizationIdAndIsReleaseIsTrueOrderByCreateTimeDesc(Long userCurrentOrganizationId, Pageable pageable);

  @Query(value = "select s from Video s where s.id in (select " +
    "s.videoId from VideoSubscription s where s.userId = ?1 )")
  List<Video> getMySubscriptionVideos(Long id);

}
